<?php
/**
 * Created by PhpStorm.
 * User: fengyan
 * Date: 18-1-4
 * Time: 下午2:59
 */

namespace App\Repositories\Addons;


use App\Repositories\Addons\Uploads\UploadsInter;

class UploadRepository
{
    /**
     * 执行操作
     * @var
     */
    private $method;

    /**
     * 执行回调方法
     * @var
     */
    private $callback;

    /**
     * UploadRepository constructor.

     * @param $method
     * @param $callback
     */
    public function __construct($method, $callback)
    {
        $this->method = $method;
        $this->callback = $callback;
    }

    /**
     * 上传处理者
     * @var
     */
    private $handler;

    /**
     * 获取实例模型
     * @return UploadsInter
     * @throws \Exception
     */
    private function getHandler($type = 'upload') : UploadsInter
    {
        if ($this->handler instanceof UploadsInter) {
            return $this->handler;
        }

        $config = config('upload');

        $drive = $config['drive'];

        $fieldName = $prefix = '';
        $uploadConfig = [];
        switch ($type) {
            case 'config':
                $uploadConfig = $config;
                $fieldName = '';
                $prefix = '';
                break;

            case 'upload':
                $prefix = $config['actionPrefixKeyList'][$this->method];

                $uploadConfig = $config[$prefix];

                $fieldName = $uploadConfig['fieldName'];

                if (!isset($_FILES[$fieldName])) {
                    throw new \Exception("未获取到上传信息");
                }
                break;

            case 'list':
                $prefix = $config['actionPrefixKeyList'][$this->method];

                $uploadConfig = $config[$prefix];
                break;
        }

        $this->handler = new $drive($uploadConfig, $fieldName, $config['basePath'], $prefix, $this->callback);

        return $this->handler;
    }

    /**
     * 获取配置
     * @return mixed
     * @throws \Exception
     */
    public function getConfig() : array
    {
        $handler = $this->getHandler('config');

        $method = $this->method;

        if (!method_exists($handler, $method)) {
            throw new \Exception("调用的方法[".$method."]名不存在");
        }

        return $handler->config()->getResult()->getData();
    }

    /**
     * 上传
     * @return array
     * @throws \Exception
     */
    public function upload() : array
    {
        $handler = $this->getHandler('upload');

        $method = $this->method;

        if (!method_exists($handler, $method)) {
            throw new \Exception("调用的方法[".$method."]名不存在");
        }

        //  调用上传方法
        $handler->$method();

        //  获取上传结果
        $result = $handler->getResult();

        //  获取上传原始配置
        $config = $this->getHandler()->getConfig();

        switch ($config['plugins']) {
            //  默认百度上传插件
            default :
                if ($result->getCode() != 0) {
                    $res = [
                        'state' => 'ERROR',
                        'msg' => $result->getErrorMsg(),
                    ];
                } else {
                    $res = [
                        "state" => "SUCCESS",
                        "url"   => $result->getData()->getLinks(),
                        "title" => $result->getData()->getTitle(),
                        "original" => $result->getData()->getOriginal(),
                        "type" => ".".$result->getData()->getExtension(),
                        "size" => $result->getData()->getSize()
                    ];
                }
                break;

        }

        return $res;
    }

    /**
     * 输出列表
     * @return array
     */
    public function lists($start, $size) : array
    {
        $handler = $this->getHandler('list');

        $method = $this->method;

        $handler->$method($start, $size);

        return $handler->getResult()->getData();
    }

    /**
     * 把文件信息保存至数据库
     */
    public function save()
    {

    }
}